package se.jayway.opengl.tutorial.mesh;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;

public class MyGrid extends Figure {
	IntBuffer vertices;
	int[] tmpBuffer;
	boolean hasColor = false;
	boolean hasTexCoords = false;
	int vertexSize = 0;
	public MyGrid() {
		float[] vtxGrid = { -10.0f, 0.0f, 0.0f, 
				10.0f, 0.0f, 0.0f, 
				0.0f, 0.0f, -10.0f,
				0.0f, 0.0f, 10.0f };


		vertexSize = (3 + (hasColor ? 4 : 0) + (hasTexCoords ? 2 : 0)) * 4;
		int maxVertices = vtxGrid.length * 4 / vertexSize;
		tmpBuffer = new int[vtxGrid.length];
		ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize);
		buffer.order(ByteOrder.nativeOrder());
		vertices = buffer.asIntBuffer();

		setVertices(vtxGrid);

	}
	
	protected void setVertices(float[] vertices) {
		this.vertices.clear();
		int len = 0 + vertices.length;
		for (int i = 0, j = 0; i < len; i++, j++)
			tmpBuffer[j] = Float.floatToRawIntBits(vertices[i]);
		this.vertices.put(tmpBuffer, 0, vertices.length);
		this.vertices.flip();
	}

	
	@Override
	public void draw(GL10 gl) {

		vertices.position(0);
		gl.glVertexPointer(3, GL10.GL_FLOAT, vertexSize, vertices);
		gl.glColor4f(.5f, .5f, .5f, 1);
		for (float f= -10; f<=10; f++){
			gl.glLoadIdentity();
			gl.glTranslatef(0, 0, f);
			gl.glDrawArrays(GL10.GL_LINES, 0, 2);
			
			gl.glLoadIdentity();
			gl.glTranslatef(f, 0, 0);
			gl.glDrawArrays(GL10.GL_LINES, 2, 2);
		}		
	}

	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		
	}

}
